package com.example.excel.service;

import com.example.excel.dao.StuDao;
import com.example.excel.entity.Student;
import com.example.excel.util.GetSqlSession;
import com.example.excel.util.MybatisUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


//业务层
public class StudentService {

    public List<Student> findAll() {

        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }

        List<Student> Students = null;
        if (stuDao != null) {
            Students = stuDao.findAll();
        } else {
            System.out.println("DAO层错误");
        }

        MybatisUtils.closeSession(sqlSession);

        return Students;

    }


    public void addStudent(Student student) {
        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }

        if (stuDao != null) {
            stuDao.addStudent(student);
        } else {
            System.out.println("DAO层错误");
        }
        if (sqlSession != null) {
            MybatisUtils.closeSession(sqlSession);
        }
    }

    public int countForName(String name) {
        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }
        int a = 0;
        if (stuDao != null) {
            a = stuDao.countForName(name);
        } else {
            System.out.println("DAO层错误");
        }
        if (sqlSession != null) {
            MybatisUtils.closeSession(sqlSession);
        }

        return a;
    }

    public int countForPwd(String pwd){
        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }
        int a = 0;
        if (stuDao != null) {
            a = stuDao.countForPwd(pwd);
        } else {
            System.out.println("DAO层错误");
        }
        if (sqlSession != null) {
            MybatisUtils.closeSession(sqlSession);
        }

        return a;
    }

    public int countForLogin(String name,String pwd){
        int a = 0;
        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }

        if (stuDao != null) {
            a = stuDao.countForLogin(name,pwd);
        } else {
            System.out.println("DAO层错误");
        }
        if (sqlSession != null) {
            MybatisUtils.closeSession(sqlSession);
        }

        return a;
    }

    public Student oneStudent(@Param("name") String name, @Param("pwd")  String pwd){
        Student student = new Student();
        SqlSession sqlSession = GetSqlSession.createSqlSession();
        StuDao stuDao = null;
        if (sqlSession != null) {
            stuDao = sqlSession.getMapper(StuDao.class);
        } else {
            System.out.println("获取会话工厂失败");
        }
        if (stuDao != null) {
            student = stuDao.oneStudent(name,pwd);
        } else {
            System.out.println("DAO层错误");
        }
        if (sqlSession != null) {
            MybatisUtils.closeSession(sqlSession);
        }

        return student;
    }

}
